package com.ruoyi.vbze.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.vbze.domain.Plant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.vbze.mapper.LeafMapper;
import com.ruoyi.vbze.domain.Leaf;
import com.ruoyi.vbze.service.ILeafService;

/**
 * 叶片管理Service业务层处理
 *
 * @author vbze
 * @date 2023-02-22
 */
@Service
public class LeafServiceImpl implements ILeafService {



    private static final Logger log = LoggerFactory.getLogger(LeafServiceImpl.class);


    @Autowired
    private LeafMapper leafMapper;

    /**
     * 查询叶片管理
     *
     * @param id 叶片管理主键
     * @return 叶片管理
     */
    @Override
    public Leaf selectLeafById(Long id) {
        return leafMapper.selectLeafById(id);
    }

    /**
     * 查询叶片管理列表
     *
     * @param leaf 叶片管理
     * @return 叶片管理
     */
    @Override
    public List<Leaf> selectLeafList(Leaf leaf) {
        return leafMapper.selectLeafList(leaf);
    }

    /**
     * 新增叶片管理
     *
     * @param leaf 叶片管理
     * @return 结果
     */
    @Override
    public int insertLeaf(Leaf leaf) {
        leaf.setCreateTime(DateUtils.getNowDate());
        return leafMapper.insertLeaf(leaf);
    }

    /**
     * 修改叶片管理
     *
     * @param leaf 叶片管理
     * @return 结果
     */
    @Override
    public int updateLeaf(Leaf leaf) {
        return leafMapper.updateLeaf(leaf);
    }

    /**
     * 批量删除叶片管理
     *
     * @param ids 需要删除的叶片管理主键
     * @return 结果
     */
    @Override
    public int deleteLeafByIds(Long[] ids) {
        return leafMapper.deleteLeafByIds(ids);
    }

    /**
     * 删除叶片管理信息
     *
     * @param id 叶片管理主键
     * @return 结果
     */
    @Override
    public int deleteLeafById(Long id) {
        return leafMapper.deleteLeafById(id);
    }


    /**
     * 导入叶片数据
     *
     * @param leafList        叶片列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importLeaf(List<Leaf> leafList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(leafList) || leafList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (Leaf leaf : leafList) {
            try {
                // 验证是否存在这个叶片
//                SysUser u = userMapper.selectUserByUserName(block.getUserName());
                Leaf l = leafMapper.selectLeafByPlantNumberAndLeafPlace(leaf.getPlantNumber(),leaf.getLeafPlace());
                if (StringUtils.isNull(l)) {
//                    BeanValidators.validateWithException(validator, block);
//                    block.setPassword(SecurityUtils.encryptPassword(password));
                    leaf.setCreateBy(operName);
                    leaf.setLastEditor(operName);
                    leaf.setUpdateBy(operName);
                    this.insertLeaf(leaf);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、植株 " + leaf.getPlantNumber() + " 第 " + leaf.getLeafPlace() + " 位的叶片导入成功");
                } else if (isUpdateSupport) {
//                    BeanValidators.validateWithException(validator, block);
//                    checkUserAllowed(block);
//                    checkUserDataScope(block.getUserId());
//                    block.setUpdateBy(operName);
                    leaf.setUpdateBy(operName);
                    leaf.setLastEditor(operName);
                    this.updateLeaf(leaf);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、植株 " + leaf.getPlantNumber() + " 第 " + leaf.getLeafPlace() + " 位的叶片更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、植株 " + leaf.getPlantNumber() + " 第 " + leaf.getLeafPlace() + " 位的叶片已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum +"、植株 " + leaf.getPlantNumber() + " 第 " + leaf.getLeafPlace() + " 位的叶片导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
